package com.mallcloud.mall.mall.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mallcloud.mall.common.core.util.R;
import com.mallcloud.mall.common.excel.annotation.ResponseExcel;
import com.mallcloud.mall.mall.api.entity.MallUserAddrModel;
import com.mallcloud.mall.mall.service.MallUserAddrService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "mall-用户地址")
@RestController
@RequiredArgsConstructor
@RequestMapping("/mall/userAddr")
public class MallUserAddrController {
	private final MallUserAddrService sysUserAddrService;

	@ApiOperation("分页")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('mall_useraddr_view')")
	public R page(Page page, MallUserAddrModel sysUserAddr) {
		return R.ok(sysUserAddrService.page(page, Wrappers.query(sysUserAddr)));
	}

	@ApiOperation("列表")
	@GetMapping("/list")
	@PreAuthorize("@pms.hasPermission('mall_useraddr_view')")
	public R list(MallUserAddrModel sysUserAddr) {
		return R.ok(sysUserAddrService.list(Wrappers.query(sysUserAddr)));
	}

	@ApiOperation("查询详情")
	@GetMapping("/{addrId}")
	@PreAuthorize("@pms.hasPermission('mall_useraddr_view')")
	public R getById(@PathVariable("addrId") Long addrId) {
		return R.ok(sysUserAddrService.getById(addrId));
	}

	@ApiOperation("新增")
	@PostMapping("/save")
	@PreAuthorize("@pms.hasPermission('mall_useraddr_add')")
	public R save(@RequestBody MallUserAddrModel sysUserAddr) {
		return R.ok(sysUserAddrService.save(sysUserAddr));
	}

	@ApiOperation("修改")
	@PostMapping("/update")
	@PreAuthorize("@pms.hasPermission('mall_useraddr_edit')")
	public R updateById(@RequestBody MallUserAddrModel sysUserAddr) {
		return R.ok(sysUserAddrService.updateById(sysUserAddr));
	}

	@ApiOperation("删除")
	@DeleteMapping("/{addrId}")
	@PreAuthorize("@pms.hasPermission('mall_useraddr_del')")
	public R removeById(@PathVariable Long addrId) {
		return R.ok(sysUserAddrService.removeById(addrId));
	}

	@ApiOperation("批量删除")
	@DeleteMapping
	@PreAuthorize("@pms.hasPermission('mall_useraddr_del')")
	public R removeByIds(@RequestBody Long[] addrIds) {
		return R.ok(sysUserAddrService.removeByIds(CollUtil.toList(addrIds)));
	}

	@ApiOperation("导出")
	@ResponseExcel
	@GetMapping("/export")
	@PreAuthorize("@pms.hasPermission('mall_useraddr_export')")
	public List<MallUserAddrModel> export(MallUserAddrModel sysUserAddr) {
		return sysUserAddrService.list(Wrappers.query(sysUserAddr));
	}
}
